package com.view.resourceLoader
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class ResourceLoaderData
	{
		private var _path:String;
		
		private var _key:String;
		
		private var _loader:Loader;

		private var _appDomain:ApplicationDomain; 
		
		private var _loadOver:Boolean;

		private var _callBack:Function = new Function();
		
		private var _swf:DisplayObject;
		
		public function ResourceLoaderData(path:String,key:String)
		{
			_path = path;
			_key = key;
			init();
		}
		
		private function init():void{
			_loader = new Loader();
			
			var context:LoaderContext = new LoaderContext();
			context.applicationDomain = ApplicationDomain.currentDomain;
			
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderCompleteHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoaderErrorHandler);
			_loader.load(new URLRequest(_path),context);	
		}
		
		private function onLoaderErrorHandler(e:IOErrorEvent):void
		{
			trace('加载出错:' + e);
		}
		
		private function onLoaderCompleteHandler(e:Event):void
		{
			_appDomain = _loader.contentLoaderInfo.applicationDomain as ApplicationDomain;
			
			_swf = _loader.content;
				
			_loadOver = true; 
			
			_callBack();
			
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoaderCompleteHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onLoaderErrorHandler);
			_loader = null;
		}	
		
		public function getDefinition(clsName:String):Class{
			return _appDomain.getDefinition(clsName) as Class;
		}
		
		public function dispose():void{
			_appDomain = null;
		}
		
		public function get loadOver():Boolean{return _loadOver;}
		
		public function get path():String{return _path;}
		
		public function get key():String{return _key;}
		
		public function set callBack(value:Function):void{_callBack = value;}
	}
}